<?php


namespace Tbf\Bundle\AlipayBundle\Api;


use Guzzle\Http\Client;
use Guzzle\Http\Url;
use Psr\Log\LoggerInterface;
use Symfony\Component\CssSelector\CssSelector;
use Symfony\Component\DomCrawler\Crawler;
use Tbf\Bundle\AlipayBundle\Config;
use Tbf\Bundle\AlipayBundle\Model\AlipaySign;
use Tbf\Bundle\AlipayBundle\Model\SendGoodsConfirmByPlatFormRequest;
use Tbf\Bundle\AlipayBundle\Model\SendGoodsConfirmByPlatFormResponse;
use Tbf\Component\Arrays\ArrayFunction;

class SendGoodsConfirmByPlatform {
    protected $http_client;
    protected $config;
    protected $logger;
    function __construct(Client $http_client,
                         Config $config,
                         LoggerInterface $logger){
        $this->http_client = $http_client;
        $this->config = $config;
        $this->logger = $logger;
    }
    function request(SendGoodsConfirmByPlatFormRequest $request){
        if (!$request->getAlipayTradeId()){
            return new SendGoodsConfirmByPlatFormResponse('require parameter alipay_trade_id');
        }
        $request_data = array(
            '_input_charset'=>'utf-8',
            'service'=>'send_goods_confirm_by_platform',
            'partner'=>$this->config->getAppKey(),
            'sign_type'=>'MD5',
            'trade_no'=>$request->getAlipayTradeId(),
            'logistics_name'=>$request->getLogisticsName(),
            'invoice_no'=>$request->getInvoiceNo(),
            'create_transport_type'=>'EXPRESS',
            'seller_ip'=>$request->getSellerIp(),
        );
        $request_data = AlipaySign::md5SignArray($request_data,$this->config->getAppSecret());
        $request_url = Url::factory($this->config->getAlipayGateway());
        $request_url->setQuery($request_data);
        $url = (string)$request_url;
        try{
            $http_response = $this->http_client->get($url)->send();
        }catch(\Exception $e){
            $this->logger->error('SendGoodsConfirmByPlatform::request catch a http exception',array(
                'url'=>$url,
                'requset_data'=>$request_data,
                'exception'=>$e
            ));
            return new SendGoodsConfirmByPlatFormResponse('http request error');
        }
        $this->logger->debug('SendGoodsConfirmByPlatform::request http finish',array(
            'url'=>$url,
            'requset_data'=>$request_data,
            'response'=>(string)$http_response,
        ));
        $xml_content = $http_response->getBody(true);
        $crawler = new Crawler();
        $crawler->addContent($xml_content,$http_response->getContentType());
        $is_success = $crawler->filter('is_success')->text();
        if ($is_success==='F'){
            $error = $crawler->filter('error')->text();
            return new SendGoodsConfirmByPlatFormResponse($error);
        }
        $resp = new SendGoodsConfirmByPlatFormResponse();
        CssSelector::disableHtmlExtension();

        $resp->setMoney($crawler->filter('response>tradeBase>total_fee')->text());
        $resp->setAlipayTradeId($crawler->filter('response>tradeBase>trade_no')->text());
        $resp->setTradeId($crawler->filter('response>tradeBase>out_trade_no')->text());
        $resp->setTradeStatus($crawler->filter('response>tradeBase>trade_status')->text());

        return $resp;
    }
}